package com.cefa.edu.controller;

import com.cefa.edu.common.result.FailureResult;
import com.cefa.edu.common.result.Result;
import com.cefa.edu.common.result.ReturnMessage;
import com.cefa.edu.common.result.SuccessResult;
import com.cefa.edu.pojo.Course;
import com.cefa.edu.pojo.Score;
import com.cefa.edu.pojo.StudentInformation;
import com.cefa.edu.pojo.UserOrganRole;
import com.cefa.edu.service.FrameworkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("framework")
public class FrameworkController {

    private final static Logger logger = LoggerFactory.getLogger(FrameworkController.class);

    @Autowired
    private FrameworkService frameworkService;

    /**
     * @param userOrganRole
     * @return 查询架构信息(老师架构和学生架构)
     */
    @RequestMapping(path = "structure", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Object> getFrameworkOfTeacher(UserOrganRole userOrganRole) {
        if (userOrganRole.getUserId() == null) {
            return new FailureResult<>(new ReturnMessage("201", "UserId参数不能为空!"));
        }
        try {
            List<Map<String, Object>> frameworkOfTeacher = frameworkService.getFrameworkOfTeacher(userOrganRole);
            return new SuccessResult<>(new ReturnMessage("200", "success"), frameworkOfTeacher);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()));
        }
    }

    /**
     * @param score
     * @param schoolId
     * @param loginUserId
     * @return s保存学生分数
     */
    @RequestMapping(path = "saveStudentScore/{schoolId}/{loginUserId}", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Score> saveStudentScore(Score score, @PathVariable Integer schoolId, @PathVariable Integer loginUserId) {
        if (frameworkService.checkStudentScoreExist(score)>0) {
            return new FailureResult<>(new ReturnMessage("201", "分数重复!"));
        }
        try {
            Score studentScore = frameworkService.saveStudentScore(score, schoolId, loginUserId);
            return new SuccessResult<>(new ReturnMessage("200", "success"), studentScore);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()));
        }
    }


    /**
     * @param studentInformation
     * @return s根据学生的姓名查询学生信息
     */
    @RequestMapping(path = "searchStudent", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Object> searchStudent(StudentInformation studentInformation) {
        if (studentInformation.getLoginUserId() == null || studentInformation.getSchoolId() == null) {
            return new FailureResult<>(new ReturnMessage("201", "loginUserId和SchoolId参数不能为空!"));
        }
        try {
            List<Map<String, Object>> students = frameworkService.searchStudent(studentInformation);
            return new SuccessResult<>(new ReturnMessage("200", "success"), students);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()));
        }
    }

    /**
     * @param studentInformation
     * @return s根据年级id查询此年级下面的课程
     */
    @RequestMapping(path = "searchCourses", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Object> searchCourses(StudentInformation studentInformation) {
        if (studentInformation.getGradeId() == null || studentInformation.getSchoolId() == null) {
            return new FailureResult<>(new ReturnMessage("201", "gradeId和SchoolId参数不能为空!"));
        }
        try {
            List<Course> courses = frameworkService.searchCourses(studentInformation);
            if(courses==null ||courses.size()==0){
                Map<String,Object> resultMap=new HashMap<>();
                resultMap.put("msg","未获取到科目数据！");
                return new SuccessResult<>(new ReturnMessage("200", "success"), resultMap);
            }else {
                return new SuccessResult<>(new ReturnMessage("200", "success"), courses);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()));
        }
    }


}
